<?php
/**
 * Created by PhpStorm.
 * User: zhouyunzhou
 * Date: 2018/7/9
 * Time: 17:32
 */

namespace AMap;

use think\Log;


class AMapApiClient
{
    private $api;

    function __construct()
    {
        $this->api = new AMapApi();
    }

    /**
     * 查询行政区域
     * @param $keywords
     * @param int $subdistrict
     * @param string $extensions
     * @return mixed
     */
    public function queryDistrict($keywords, $subdistrict = 0, $extensions = 'base')
    {
        $apiPath = AMapApiConstent::AMAP_API_DISTRICT;
        $data = ['keywords' => $keywords,
            'subdistrict' => $subdistrict,
            'extensions' => $extensions
        ];
        Log::debug('AMapApi queryDistrict,params:' . json_encode($data));
        return $this->api->get($apiPath, $data);
    }

    /**
     * 查询天气信息
     * @param $cityCode
     * @param string $extensions
     * @param string $output
     * @return mixed
     */
    public function queryWeather($cityCode, $extensions = 'all', $output = 'JSON')
    {
        $apiPath = AMapApiConstent::AMAP_API_WEATHER;
        $data = ['city' => $cityCode,
            'extensions' => $extensions,
            'output' => $output
        ];
        Log::debug('AMapApi queryWeather,params:' . json_encode($data));
        return $this->api->get($apiPath, $data);
    }


    /**
     * 根据经纬度查询地址信息
     * @param $lat
     * @param $lng
     * @return mixed
     */
    public function queryLocation($lat, $lng)
    {
        $apiPath = AMapApiConstent::AMAP_API_REGEO;
        $data = [
            'location' => round($lng, 6) . ',' . round($lat, 6)
        ];
        Log::debug('AMapApi queryLocation,params:' . json_encode($data));
        return $this->api->get($apiPath, $data);
    }


    /**
     * 转换坐标为火星坐标系
     * @param $locations
     * @param string $coordsys
     * @return mixed
     */
    public function convertLnglats($locations, $coordsys = AMapApiConstent::COORDSYS_TYPE_GPS)
    {
        if (is_array($locations)) {
            $locations = implode('|', $locations);
        }

        $apiPath = AMapApiConstent::AMAP_API_CONVERT;
        $data = [
            'locations' => $locations,
            'coordsys' => $coordsys
        ];
        Log::debug('AMapApi convertLnglat,params:' . json_encode($data));
        return $this->api->get($apiPath, $data);
    }


    /**
     * 根据ip查询地址信息
     * @param $ip
     * @return mixed
     */
    public function queryIpLocation($ip)
    {
        if (empty($ip)) {
            return false;
        }
        $apiPath = AMapApiConstent::AMAP_API_IP_LOCATION;
        $data = [
            'ip' => $ip
        ];
        Log::debug('AMapApi queryIpLocation,params:' . json_encode($data));
        return $this->api->get($apiPath, $data);
    }
}